package com.westos.calcv3.proxy;

import com.westos.calcv2.IOperator;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class ProxyHandler implements InvocationHandler {
    private Object obj;
    public ProxyHandler(Object realObject){
        this.obj=realObject;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("方法执行前");
        //进行参数检查
        if(this.obj instanceof IOperator){
            if(args.length!=2){
                System.out.println("参数个数不正确");
                return null;
            }
            if(args[0]==null||args[1]==null){
                System.out.println("参数不能为空");
                return null;
            }
        }
        Object result = method.invoke(obj, args);
        System.out.println("方法执行后");
        return result;
    }
}